package com.wufun.overseas.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wufun.overseas.R;
import com.wufun.overseas.models.bean.GameTransferBean;
import com.wufun.overseas.models.bean.GameTransferStuatusBean;
import com.wufun.overseas.utils.MyImageLoader;
import com.wufun.overseas.utils.StringUtils;

import java.io.File;

/**
 * Author: hest
 * Date:  2016/5/5
 */
public class GameTransferHistoryListItem extends RelativeLayout {
    private static final String TAG = GameTransferHistoryListItem.class.getSimpleName();

    private TextView game_name_txt;
    private TextView game_size_txt;
    private TextView transfer_txt;
    private SimpleDraweeView icon_img;

    GameTransferBean mBean;
    Context mContext;

    public GameTransferHistoryListItem(Context context) {
        super(context);
        mContext = context;
        initView(context);
    }

    private void initView(Context context) {
        View.inflate(context, R.layout.game_transfer_list_item, GameTransferHistoryListItem.this);
        game_name_txt = (TextView) this.findViewById(R.id.game_transfer_item_game_name_txt);
        game_size_txt = (TextView) this.findViewById(R.id.game_transfer_item_game_size_txt);
        transfer_txt = (TextView) this.findViewById(R.id.game_transfer_list_item_transfer_status_txt);
        icon_img = (SimpleDraweeView) this.findViewById(R.id.game_transfer_item_game_icon_img);
    }

    public void bind(GameTransferBean bean) {
        if (bean == null) {
            return;
        }
        mBean = bean;
        game_name_txt.setText(bean.getGameName());
        game_size_txt.setText(bean.getSize() + "M");
        MyImageLoader.load(icon_img,R.drawable.main_normal_icon,mBean.getIconUrl());
        Log.d(TAG, "bind:  gameName:::" + game_name_txt.getText().toString() + "::gameSize:::" + game_size_txt.getText().toString());
        transfer_txt.setOnClickListener(null);
        transfer_txt.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View view) {
                if (mBean != null) {
                    String filePath = getApkPath(mBean.getId());
                    if (!StringUtils.isEmpty(filePath)) {
                        apkInstal(filePath);
                    }
                }
            }
        });
        setTransferStatus();
    }

    //设置传输状态
    void setTransferStatus() {
        int status = mBean.getTransferStatus();
        transfer_txt.setVisibility(VISIBLE);
        transfer_txt.setClickable(false);
        //背景全透明
        transfer_txt.setBackgroundColor(new Color().alpha(0));
        switch (status) {
            case GameTransferStuatusBean.WAITING:
                transfer_txt.setText(R.string.waiting_status_game_transfer);
                transfer_txt.setTextColor(mContext.getResources().getColor(R.color.black_game_transfer_game_size));
                break;
            case GameTransferStuatusBean.TRANSFERING:
                transfer_txt.setText(mBean.getProgerss() + "%");
                transfer_txt.setTextColor(mContext.getResources().getColor(R.color.blue_game_transfer));
                break;
            case GameTransferStuatusBean.TRANSFER_SUCCESS:
                transfer_txt.setText(R.string.game_transfer_success);
                transfer_txt.setTextColor(mContext.getResources().getColor(R.color.blue_game_transfer));
                break;
            case GameTransferStuatusBean.TRANSFER_FAILED:
                transfer_txt.setTextColor(Color.RED);
                transfer_txt.setText(R.string.transfer_failed_status_game_transfer);
                break;
            case GameTransferStuatusBean.UNZIP:
                transfer_txt.setText(R.string.transfer_unzip_status_game_transfer);
                transfer_txt.setTextColor(mContext.getResources().getColor(R.color.blue_game_transfer));
                break;
            case GameTransferStuatusBean.INSTALL:
                transfer_txt.setBackgroundResource(R.drawable.bg_game_transfer_install_app_txt);
                transfer_txt.setText(R.string.transfer_install_status_game_transfer);
                transfer_txt.setTextColor(mContext.getResources().getColor(R.color.app_green_color));
                transfer_txt.setClickable(true);
                break;
            case GameTransferStuatusBean.FAILED:
                transfer_txt.setTextColor(Color.RED);
                transfer_txt.setText(R.string.transfer_failed_status_game_transfer);
                break;
            case GameTransferStuatusBean.FINISH:
                transfer_txt.setText(R.string.transfer_success_status_game_transfer);
                transfer_txt.setTextColor(mContext.getResources().getColor(R.color.blue_game_transfer));
                break;
            default:
                break;
        }
    }


    String getApkPath(String id) {
//        DownloadTask task = DownloadTaskManager.getInstance().getByGameId(id);
//        if (task == null) {
//            return null;
//        }
//        String apkPath = task.getGameZipPath();
//        return apkPath;
        return "";
    }


    void apkInstal(String apkpath) {
        Intent intentx = new Intent(Intent.ACTION_VIEW);
        intentx.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intentx.setAction(Intent.ACTION_VIEW);
        File fileApk = new File(apkpath);
        intentx.setDataAndType(Uri.fromFile(fileApk), "application/vnd.android.package-archive");
        mContext.startActivity(intentx);
    }
}
